﻿/*
Copyright (c) 2014, Lars Brubaker
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/


namespace Net3dBool
{
    public class NblPlane
    {
        public double DistanceToPlaneFromOrigin;
        public NblVec3 PlaneNormal;
        private const double TreatAsZero = .000000001;

        public NblPlane(NblVec3 planeNormal, double distanceFromOrigin)
        {
            PlaneNormal = planeNormal.Normalized();
            DistanceToPlaneFromOrigin = distanceFromOrigin;
        }

        public NblPlane(NblVec3 point0, NblVec3 point1, NblVec3 point2)
        {
            PlaneNormal = NblVec3.Cross(point1 - point0, point2 - point0).Normalized();
            DistanceToPlaneFromOrigin = NblVec3.Dot(PlaneNormal, point0);
        }

        public NblPlane(NblVec3 planeNormal, NblVec3 pointOnPlane)
        {
            PlaneNormal = planeNormal.Normalized();
            DistanceToPlaneFromOrigin = NblVec3.Dot(planeNormal, pointOnPlane);
        }

        public double GetDistanceFromPlane(NblVec3 positionToCheck)
        {
            double distanceToPointFromOrigin = NblVec3.Dot(positionToCheck, PlaneNormal);
            return distanceToPointFromOrigin - DistanceToPlaneFromOrigin;
        }

        public double GetDistanceToIntersection(NblRay ray, out bool inFront)
        {
            inFront = false;
            double normalDotRayDirection = NblVec3.Dot(PlaneNormal, ray.DirectionNormal);
            if (normalDotRayDirection < TreatAsZero && normalDotRayDirection > -TreatAsZero) // the ray is parallel to the plane
            {
                return double.PositiveInfinity;
            }

            if (normalDotRayDirection < 0)
            {
                inFront = true;
            }

            return (DistanceToPlaneFromOrigin - NblVec3.Dot(PlaneNormal, ray.Origin)) / normalDotRayDirection;
        }

        public double GetDistanceToIntersection(NblVec3 pointOnLine, NblVec3 lineDirection)
        {
            double normalDotRayDirection = NblVec3.Dot(PlaneNormal, lineDirection);
            if (normalDotRayDirection < TreatAsZero && normalDotRayDirection > -TreatAsZero) // the ray is parallel to the plane
            {
                return double.PositiveInfinity;
            }

            double planeNormalDotPointOnLine = NblVec3.Dot(PlaneNormal, pointOnLine);
            return (DistanceToPlaneFromOrigin - planeNormalDotPointOnLine) / normalDotRayDirection;
        }

        public bool RayHitPlane(NblRay ray, out double distanceToHit, out bool hitFrontOfPlane)
        {
            distanceToHit = double.PositiveInfinity;
            hitFrontOfPlane = false;

            double normalDotRayDirection = NblVec3.Dot(PlaneNormal, ray.DirectionNormal);
            if (normalDotRayDirection < TreatAsZero && normalDotRayDirection > -TreatAsZero) // the ray is parallel to the plane
            {
                return false;
            }

            if (normalDotRayDirection < 0)
            {
                hitFrontOfPlane = true;
            }

            double distanceToRayOriginFromOrigin = NblVec3.Dot(PlaneNormal, ray.Origin);

            double distanceToPlaneFromRayOrigin = DistanceToPlaneFromOrigin - distanceToRayOriginFromOrigin;

            bool originInFrontOfPlane = distanceToPlaneFromRayOrigin < 0;

            bool originAndHitAreOnSameSide = originInFrontOfPlane == hitFrontOfPlane;
            if (!originAndHitAreOnSameSide)
            {
                return false;
            }

            distanceToHit = distanceToPlaneFromRayOrigin / normalDotRayDirection;
            return true;
        }

        public bool LineHitPlane(NblVec3 start, NblVec3 end, out NblVec3 intersectionPosition)
        {
            double distanceToStartFromOrigin = NblVec3.Dot(PlaneNormal, start);
            if (distanceToStartFromOrigin == 0)
            {
                intersectionPosition = start;
                return true;
            }

            double distanceToEndFromOrigin = NblVec3.Dot(PlaneNormal, end);
            if (distanceToEndFromOrigin == 0)
            {
                intersectionPosition = end;
                return true;
            }

            if ((distanceToStartFromOrigin < 0 && distanceToEndFromOrigin > 0)
                || (distanceToStartFromOrigin > 0 && distanceToEndFromOrigin < 0))
            {
                NblVec3 direction = (end - start).Normalized();

                double startDistanceFromPlane = distanceToStartFromOrigin - DistanceToPlaneFromOrigin;
                double endDistanceFromPlane = distanceToEndFromOrigin - DistanceToPlaneFromOrigin;
                double lengthAlongPlanNormal = endDistanceFromPlane - startDistanceFromPlane;

                double ratioToPlanFromStart = startDistanceFromPlane / lengthAlongPlanNormal;
                intersectionPosition = start + direction * ratioToPlanFromStart;

                return true;
            }

            intersectionPosition = NblVec3.PositiveInfinity;
            return false;
        }
    }
}